/*
 * "Hello World" example.
 *
 * This example prints 'Hello from Nios II' to the STDOUT stream. It runs on
 * the Nios II 'standard', 'full_featured', 'fast', and 'low_cost' example
 * designs. It runs with or without the MicroC/OS-II RTOS and requires a STDOUT
 * device in your system's hardware.
 * The memory footprint of this hosted application is ~69 kbytes by default
 * using the standard reference design.
 *
 * For a reduced footprint version of this template, and an explanation of how
 * to reduce the memory footprint for a given application, see the
 * "small_hello_world" template.
 *
 */

#include <stdio.h>
#include "io.h"
#include "system.h"
#include "Pwm.h"
#include "BUS.h"
#include "LED.h"
#include "key4x4.h"
#include "lcd.h"
#include "Menu.h"
extern alt_u32 flag1;
float date_sine_float,date_AM_float,date_FM_float;
alt_u32 date_sine,date_AM,date_FM,modulation,k,b,offset;
int main()
{
  printf("Hello from Nios II!\n");
  int a;
  a=KEY_init();
  printf("%d",a);
  GetFreq();
  Lcd_Init();
  DispString67At(0,0,"nihao",1);
  usleep(1000000);
  DispMainMenu();
  while(1){
	  if(KeyValidFlag){
		  KeyValidFlag=0;
		  Menu();
		    if(flag1==3)
		    {
		    	date_sine_float=(float)(SIGNAL.sine.freq)*429.4967295;
		    	date_sine=(alt_u32)date_sine_float;
	//	    	Volt=(SIGNAL.sine.vpp_mv)
		    	IOWR(DDS_SINE_INTERFACE_BASE,1,date_sine);
	//	    	IOWR(DDS_SINE_INTERFACE_BASE,0,Volt);
		    	IOWR(CHARGE_BASE,1,3);
		    }
		    if(flag1==4)
		    {
		    	date_AM_float=(float)(SIGNAL.AM.freq_mod)*429.4967295;
		    	date_AM=(alt_u32)date_AM_float;
		    	IOWR(DDS_AM_INTERFACE_BASE,1,date_AM);
		    	k=100-(SIGNAL.AM.ma);
		    	IOWR(DDS_AM_INTERFACE_BASE,2,k);
		    	b=16383*(SIGNAL.AM.ma)/100;
		    	IOWR(DDS_AM_INTERFACE_BASE,3,b);
		    	IOWR(CHARGE_BASE,1,4);
	        }
		    if(flag1==5)
		    {
		    	date_FM_float=(float)(SIGNAL.FM.freq_base)*429.4967295;
		    	date_FM=(alt_u32)date_FM_float;
		    	IOWR(DDS_FM_INTERFACE_BASE,1,date_FM);
		    	offset=(SIGNAL.FM.freq_offset)/100;
	            IOWR(DDS_FM_INTERFACE_BASE,2,offset);
	            IOWR(CHARGE_BASE,1,5);
		    }
		    if(flag1==6)
		    	IOWR(CHARGE_BASE,1,6);
		    if(flag1==7)
		    	IOWR(CHARGE_BASE,1,7);

	  }
  }

  return 0;
}
